<?php
/**
 * 账单
 */
namespace App\Validation;

use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Numericality;

class NodesValidation extends BaseValidation
{
	public function initialize()
	{
		$this->add(
			"id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"pid",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"name",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  // "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"url",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"is_display",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"is_menu",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"order_by",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"create_time",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"update_time",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);
	}

	//验证之前
	public function beforeValidation($data, $entity, $messages)
    {
    	$data->id = property_exists($data,'id') ? $data->id : NULL;
    	$data->pid = property_exists($data,'pid') ? $data->pid : 0;
    	$data->name = property_exists($data,'name') ? $data->name : NULL;
    	$data->url = property_exists($data,'url') ? $data->url : NULL;
    	$data->is_display = property_exists($data,'is_display') ? $data->is_display : 1;
    	$data->is_menu = property_exists($data,'is_menu') ? $data->is_menu : 0;
    	$data->order_by = property_exists($data,'order_by') ? $data->order_by : 100;
    	$data->create_time = property_exists($data,'create_time') ? $data->create_time : date('Y-m-d H:i:s', time());
    	$data->update_time = property_exists($data,'update_time') ? $data->update_time : date('Y-m-d H:i:s', time());
    	
        return true;
    }
}